// 权限插件
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:get/get.dart';

class PermissionController {
  // 获取麦克风权限
  static Future<bool> GetMicrophonePermission() async {
    // 获取麦克风对象
    Permission permission = Permission.microphone;
    // 获取麦克风对象状态
    PermissionStatus status = await permission.status;
    //granted 通过，denied 被拒绝，permanentlyDenied 拒绝且不在提示, restricted：表示权限受限制
    print('==========================================');
    print(status.name);
    if (status.isGranted) {
      return true;
    } else if (status.isDenied) {
      // 询问用户麦克风权限
      _RequestPermission();
    } else if (status.isPermanentlyDenied) {
      Get.defaultDialog(
          title: "权限申请",
          content: const Text('"请到设置中打开麦克风权限"'),
          onConfirm: () {
            // 打开应用程序设置页面
            openAppSettings();
            Get.back();
          });
    } else if (status.isRestricted) {
      _RequestPermission();
    }
    return false;
  }

  static _RequestPermission() async {
    // 询问用户获取麦克风权限
    await Permission.microphone.request();
  }
}
